package com.hanserwei.han_picture.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanserwei.han_picture.annotation.AuthCheck;
import com.hanserwei.han_picture.constant.UserConstant;
import com.hanserwei.han_picture.domain.common.ApiResponse;
import com.hanserwei.han_picture.domain.common.DeleteRequest;
import com.hanserwei.han_picture.domain.entity.po.User;
import com.hanserwei.han_picture.domain.entity.vo.LoginUserVO;
import com.hanserwei.han_picture.domain.entity.vo.UserVO;
import com.hanserwei.han_picture.domain.request.user.*;
import com.hanserwei.han_picture.enums.ResponseCodeEnum;
import com.hanserwei.han_picture.service.UserService;
import com.hanserwei.han_picture.utils.ThrowUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 用户(user)表控制层
 *
 * @author hanserwei
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Tag(name = "UserController")
public class UserController {

    private final UserService userServiceImpl;

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public ApiResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        ThrowUtils.throwIf(userRegisterRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        return ApiResponse.success("注册成功！", userServiceImpl.userRegister(userAccount, userPassword, checkPassword));
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public ApiResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        return ApiResponse.success("登录成功！", userServiceImpl.userLogin(userAccount, userPassword, request));
    }

    @Operation(summary = "获取当前登录用户")
    @GetMapping("/get/login")
    public ApiResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User loginUser = userServiceImpl.getLoginUser(request);
        LoginUserVO loginUserVO = userServiceImpl.getLoginUserVO(loginUser);
        return ApiResponse.success("获取当前登录用户成功！", loginUserVO);
    }

    @Operation(summary = "用户注销")
    @PostMapping("/logout")
    public ApiResponse<Boolean> userLogOut(HttpServletRequest request) {
        return ApiResponse.success("注销成功！", userServiceImpl.userLogOut(request));
    }

    @Operation(summary = "管理员添加用户")
    @PostMapping("/add")
    @AuthCheck(requiredRole = UserConstant.ADMIN_ROLE)
    public ApiResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
        ThrowUtils.throwIf(userAddRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        return ApiResponse.success("添加用户成功！", userServiceImpl.addUser(userAddRequest));
    }

    @Operation(summary = "管理员删除用户")
    @DeleteMapping("/delete")
    @AuthCheck(requiredRole = UserConstant.ADMIN_ROLE)
    public ApiResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        long id = deleteRequest.getId();
        ThrowUtils.throwIf(id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        return ApiResponse.success("删除用户成功！", userServiceImpl.deleteUser(id));
    }

    @Operation(summary = "管理员更新用户")
    @PostMapping("/update")
    @AuthCheck(requiredRole = UserConstant.ADMIN_ROLE)
    public ApiResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        ThrowUtils.throwIf(userUpdateRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        return ApiResponse.success("更新用户成功！", userServiceImpl.updateUser(userUpdateRequest));
    }

    @Operation(summary = "编辑用户")
    @PostMapping("/edit")
    public ApiResponse<Boolean> editUser(@RequestBody UserEditRequest userEditRequest) {
        ThrowUtils.throwIf(userEditRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        UserUpdateRequest userUpdateRequest = new UserUpdateRequest();
        BeanUtils.copyProperties(userEditRequest, userUpdateRequest);
        return ApiResponse.success("更新用户成功！", userServiceImpl.updateUser(userUpdateRequest));
    }

    @Operation(summary = "管理员根据Id查询用户")
    @GetMapping("/get")
    @AuthCheck(requiredRole = UserConstant.ADMIN_ROLE)
    public ApiResponse<User> getUserById(@RequestParam Long id) {
        ThrowUtils.throwIf(id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        return ApiResponse.success("查询用户成功！", userServiceImpl.getUserById(id));
    }

    @Operation(summary = "普通用户根据id查询用户")
    @GetMapping("/get/vo")
    public ApiResponse<UserVO> getUserVOById(@RequestParam Long id) {
        ThrowUtils.throwIf(id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        User user = userServiceImpl.getUserById(id);
        ThrowUtils.throwIf(ObjUtil.isNull(user), ResponseCodeEnum.USER_NOT_FOUND, "用户不存在！");
        UserVO userVO = userServiceImpl.getUserVO(user);
        return ApiResponse.success("查询用户成功！", userVO);
    }

    @Operation(summary = "管理员分页查询用户")
    @PostMapping("/list/page/vo")
    @AuthCheck(requiredRole = UserConstant.ADMIN_ROLE)
    public ApiResponse<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryRequest queryRequest) {
        ThrowUtils.throwIf(queryRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        return ApiResponse.success("查询用户成功！", userServiceImpl.getUserList(queryRequest));
    }
}
